
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Jobson,
 */
public class CadastroDisciplina extends javax.swing.JInternalFrame {

    /**
     * Creates new form CadastroDisciplina
     */
    public CadastroDisciplina() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelDic = new javax.swing.JPanel();
        cBoxCH = new javax.swing.JComboBox();
        campoQtdeVagas = new javax.swing.JFormattedTextField();
        botaoCadastrar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Cadastrar Disciplina");

        panelDic.setBorder(javax.swing.BorderFactory.createTitledBorder("Disciplina"));

        labelNome.setText("Nome:");

        labelCH.setText("Carga Horária:");

        cBoxCH.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "30", "40", "50", "60" }));

        labelQtdeVagas.setText("Quantidade de Vagas:");

        try {
            campoQtdeVagas.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        botaoCadastrar.setText("Cadastrar");
        botaoCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCadastrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelDicLayout = new javax.swing.GroupLayout(panelDic);
        panelDic.setLayout(panelDicLayout);
        panelDicLayout.setHorizontalGroup(
            panelDicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelDicLayout.createSequentialGroup()
                .addComponent(labelNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoNome))
            .addGroup(panelDicLayout.createSequentialGroup()
                .addComponent(labelCH)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cBoxCH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23)
                .addComponent(labelQtdeVagas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoQtdeVagas, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botaoCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 24, Short.MAX_VALUE))
        );
        panelDicLayout.setVerticalGroup(
            panelDicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDicLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panelDicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNome)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelDicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCH)
                    .addComponent(cBoxCH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelQtdeVagas)
                    .addComponent(campoQtdeVagas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botaoCadastrar)))
        );

        labelAviso.setForeground(new java.awt.Color(255, 51, 51));
        labelAviso.setText("<html>Aviso:<br>\nPreencha sempre a quantidade de vagas com 3 dígitos.<br>\nExempo: 2 vagas -> preencher 002</html>");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelDic, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelAviso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelDic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelAviso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCadastrarActionPerformed
        // TODO add your handling code here:
        String nome;
        int indCH, qtdevagas = 0;

        nome = campoNome.getText();
        indCH = cBoxCH.getSelectedIndex();
        if (nome.isEmpty() || indCH == -1) {
            JOptionPane.showMessageDialog(this, "Preencha todos os campos corrematente");
        } else {
            try {
                qtdevagas = Integer.parseInt(campoQtdeVagas.getText());

                if (qtdevagas <= 0) {
                    JOptionPane.showMessageDialog(this, "Quantidade deve ser no mínimo 001");
                } else {
                    Disciplina dic = new Disciplina(nome, indCH, qtdevagas);
                    Caderneta.getDisciplinas().add(dic);
                    JOptionPane.showMessageDialog(this, "Disciplina cadastrada");
                    limpaCampos();
                }
            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(this, "Preecha o campo quantidade de vagas com 3 dígitos");
                campoQtdeVagas.requestFocus();
            }
        }


    }//GEN-LAST:event_botaoCadastrarActionPerformed

    public void limpaCampos() {
        campoNome.setText("");
        cBoxCH.setSelectedIndex(-1);
        campoQtdeVagas.setText("");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoCadastrar;
    private javax.swing.JComboBox cBoxCH;
    private final javax.swing.JTextField campoNome = new javax.swing.JTextField();
    private javax.swing.JFormattedTextField campoQtdeVagas;
    private final javax.swing.JLabel labelAviso = new javax.swing.JLabel();
    private final javax.swing.JLabel labelCH = new javax.swing.JLabel();
    private final javax.swing.JLabel labelNome = new javax.swing.JLabel();
    private final javax.swing.JLabel labelQtdeVagas = new javax.swing.JLabel();
    private javax.swing.JPanel panelDic;
    // End of variables declaration//GEN-END:variables
}
